package com.alienlab.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.TimerTask;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Mytask extends TimerTask  {
	private Properties propertyConfig = new PropertyConfig("jdbc.properties").getProperties();
	private String news_url=  propertyConfig.getProperty("url");
	private String dummy_path=  propertyConfig.getProperty("dummy-path");
	@Override
	public void run() {
		Mytask mytask = new Mytask();
		try {
			mytask.startDownLoad(Mytask.getNewsData());
			System.out.println("123");
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public  static JSONArray  getNewsData() throws IOException{
		List<String> namearry= new ArrayList<>();
		List<String> urlarry= new ArrayList<>();
		int m = 0 ;
		int n = 0;
		String today = NowTime.getNowTime("yyyy-MM-dd");
		String detailtime [] = today.split("-");
		//所有的报纸的信息
        String allnews = HttpUtil.getInstance().get("https://leancloud.cn:443/1.1/classes/tb_news?order=news_no&&","X-LC-Id", "04tA7v3k4qyxjytIPrXDULgq-gzGzoHsz","X-LC-Key","laSjFrKQD7Bvf991kvcD8Sps","Content-Type", "application/json");
        //今天下载的报纸的
        String allnewsurl = HttpUtil.getInstance().get("https://leancloud.cn:443/1.1/classes/news_pdf?where=%7B%22news_time%22%3A%22"+detailtime[0]+"-"+detailtime[1]+"%2F"+detailtime[2]+"%22%7D&&&order=news_no&&","X-LC-Id", "04tA7v3k4qyxjytIPrXDULgq-gzGzoHsz","X-LC-Key","laSjFrKQD7Bvf991kvcD8Sps","Content-Type", "application/json");
        JSONObject allnewsjson = JSONObject.parseObject(allnews);
        JSONObject allnewsurljson = JSONObject.parseObject(allnewsurl);
        JSONArray allnewsjsonArray  = allnewsjson.getJSONArray("results");
        JSONArray allnewsurljsonArray = allnewsurljson.getJSONArray("results");
        int allnewsjsonArraylen = allnewsjsonArray.size();
        int allnewsurljsonArraylen = allnewsurljsonArray.size();
        System.out.println(allnewsjsonArraylen);
        System.out.println(allnewsurljsonArraylen);
        //如果
        if(allnewsjsonArraylen == allnewsurljsonArraylen ){
        	System.out.println("今天的报纸下载完成");
        }else{
            for(int i  = 0 ; i<allnewsjsonArraylen ; i ++){
            	JSONObject jsonObject = allnewsjsonArray.getJSONObject(i);
            	String news_no = jsonObject.getString("news_no");
            	String news_url = jsonObject.getString("news_url");
            	boolean flag = false;
            	for(int j = 0 ; j<allnewsurljsonArraylen ;j++ ){
            		JSONObject jsonObjecturl = allnewsurljsonArray.getJSONObject(j);
            		String news_no_url = jsonObjecturl.getString("news_no");
            		if(news_no.equals(news_no_url)){
            			flag = true;
            		}
            	}
            	if(flag){
        
            	}else{
            		namearry.add(news_no);
            		urlarry.add(news_url);
            		m++;
            		n++;
            	}
            }
        }
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(namearry);
        jsonArray.add(urlarry);
        System.out.println(namearry);
        return jsonArray;
	}
	
	public void startDownLoad(JSONArray jsonArray){
		@SuppressWarnings("unchecked")
		List<String> name = (List<String>) jsonArray.get(0);
		@SuppressWarnings("unchecked")
		List<String> url= (List<String>) jsonArray.get(1);
		NewsManage newsManage = new NewsManage();
		String getpath = ""+dummy_path+"";
		getpath = getpath.replace("\\", "\\\\");
		newsManage.getUrlAndDownLoad(name,url,getpath);	
	}
}
